package com.china08.yxyapi.api.manage.sch.platform.website.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DisposeCkContentUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ArticleDetailController extends YxyApiBaseController {

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("文章详情")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/article/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;

        SwsArticle article = swsArticleRepository.findById(id);
        Assert.isTrue(null != article, "不存在当前资讯");

        RespModel respModel = new RespModel();
        respModel.id = article.getId();
        respModel.section1Id = article.getSection1Id();
        respModel.section2Id = article.getSection2Id();
        respModel.title = article.getTitle();
        respModel.author = article.getAuthor();
        respModel.showDate = article.getShowDate();
        respModel.attachmentFileName = article.getAttachmentFileName();
        respModel.attachmentDownloadUrl = article.getAttachmentDownloadUrl();
        respModel.content = DisposeCkContentUtil.dispose(article.getContent());
        // System.out.println(DisposeCkContentUtil.dispose(article.getContent()));
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String id;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "修改资讯为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String section1Id;
        public String section2Id;
        public String title;
        public String author;
        public java.util.Date showDate;
        public String attachmentFileName;
        public String attachmentDownloadUrl;
        public String content;
    }
}
